package entity

import (
	"time"
	"xr-game-server/core/constants/db"
	"xr-game-server/core/migrate"
	"xr-game-server/core/snowflake"
	"xr-game-server/core/syndb"
)

const (
	// TbGuildMember 公会成员表名
	TbGuildMember db.TbName = "guild_members"
)

// 公会成员表列名
const (
	GuildMemberGuildId db.TbCol = "guild_id"
	GuildMemberRoleId  db.TbCol = "role_id"
	GuildMemberTitle   db.TbCol = "title"
)

// GuildMember 公会成员实体
type GuildMember struct {
	migrate.MoreModel
	GuildId uint64 `gorm:"comment:公会ID"`
	RoleId  uint64 `gorm:"comment:角色ID"`
	Title   uint8  `gorm:"default:0;comment:成员头衔"`
}

// NewGuildMember 创建公会成员
func NewGuildMember(guildId uint64, roleId uint64) *GuildMember {
	ret := &GuildMember{}

	ret.ID = snowflake.GetId()
	ret.SetGuildId(guildId)
	ret.SetRoleId(roleId)

	ret.SetCreatedAt(time.Now())
	ret.SetUpdatedAt(time.Now())

	return ret
}

// GuildMember setters

func (gm *GuildMember) SetGuildId(guildId uint64) {
	gm.GuildId = guildId
	gm.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuildMember, GuildMemberGuildId, &syndb.ColData{
		IdVal:  gm.ID,
		ColVal: guildId,
	})
}

func (gm *GuildMember) SetRoleId(roleId uint64) {
	gm.RoleId = roleId
	gm.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuildMember, GuildMemberRoleId, &syndb.ColData{
		IdVal:  gm.ID,
		ColVal: roleId,
	})
}
func (gm *GuildMember) SetTitle(title uint8) {
	gm.Title = title
	gm.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuildMember, GuildMemberTitle, &syndb.ColData{
		IdVal:  gm.ID,
		ColVal: title,
	})
}

func (gm *GuildMember) SetCreatedAt(time time.Time) {
	gm.CreatedAt = time
	syndb.AddDataToLazyChan(TbGuildMember, db.CreatedAtName, &syndb.ColData{
		IdVal:  gm.ID,
		ColVal: time,
	})
}
func (gm *GuildMember) SetUpdatedAt(time time.Time) {
	gm.UpdatedAt = time
	syndb.AddDataToLazyChan(TbGuildMember, db.UpdatedAtName, &syndb.ColData{
		IdVal:  gm.ID,
		ColVal: time,
	})
}

func (gm *GuildMember) SetDeletedAt(time *time.Time) {
	gm.DeletedAt = *time
	syndb.AddDataToLazyChan(TbGuildMember, db.DeletedAtName, &syndb.ColData{
		IdVal:  gm.ID,
		ColVal: time,
	})
}
func (gm *GuildMember) SetIsDeleted(deleted bool) {
	gm.IsDeleted = deleted
	now := time.Now()
	gm.SetUpdatedAt(now)
	gm.SetDeletedAt(&now)
	syndb.AddDataToLazyChan(TbGuildMember, db.IsDeletedName, &syndb.ColData{
		IdVal:  gm.ID,
		ColVal: deleted,
	})
}

func initGuildMember() {
	// 注册GuildMember表
	syndb.RegLazyWithMiddle(TbGuildMember, db.CreatedAtName)
	syndb.RegLazyWithMiddle(TbGuildMember, db.UpdatedAtName)
	syndb.RegLazyWithMiddle(TbGuildMember, db.IsDeletedName)
	syndb.RegLazyWithMiddle(TbGuildMember, db.DeletedAtName)
	syndb.RegLazyWithMiddle(TbGuildMember, GuildMemberGuildId)
	syndb.RegLazyWithMiddle(TbGuildMember, GuildMemberRoleId)
	syndb.RegLazyWithMiddle(TbGuildMember, GuildMemberTitle)

	migrate.AutoMigrate(&GuildMember{})
}
